<?php
	class StudentModel extends Model {
		function StudentModel()
		{
			parent::Model();			
		}
		
		function addFromC($data, $bd)
		{
			if($bd != null)
            	$this->db->set('birthday', "str_to_date('".$bd."', '%d/%m/%Y')", FALSE);
            $this->db->set('create_by', "'System-TL'", FALSE);
            $this->db->set('create_date', 'NOW()', FALSE);
            $this->db->set('is_active', '1', FALSE);  
			$this->db->insert('student', $data);
			return $this->db->insert_id();
		}
		
		function add($data)
		{
			$dataDB = array(
               	'title' => $data['title'] ,
				'first_name' => $data['first_name'] ,
				'last_name' => $data['last_name'] ,
				'nick_name' => $data['nick_name'] ,
				'current_class' => $data['current_class'] ,
				'school' => $data['school'] ,
				'plan_edu' => $data['plan_edu'] ,
				'course_type' => $data['course_type'] ,
				'citizen_id' => $data['citizen_id'] ,
				'comment' => $data['comment'] ,
				'isBacklist' => $data['isBacklist'] ,
				'blackList_comment' => $data['blackList_comment'] ,
			   	'create_by' => $this->session->userdata('username'),
				'source' => $data['source'] ,
				'status' => $data['status'] ,
			   	'is_active' => 1
            );
            if($data['birthday'] != null)
            	$this->db->set('birthday', "str_to_date('".$data['birthday']."', '%d/%m/%Y')", FALSE);
            $this->db->set('create_date', 'NOW()', FALSE); 
			$this->db->insert('student', $dataDB);
			return $this->db->insert_id();
		}
		
				
		function updateInfo($data)
		{			
			$dataDB = array(
              	'title' => $data['title'] ,
				'first_name' => $data['first_name'] ,
				'last_name' => $data['last_name'] ,
				'nick_name' => $data['nick_name'] ,
				'current_class' => $data['current_class'] ,
				'school' => $data['school'] ,
				'plan_edu' => $data['plan_edu'] ,
				'course_type' => $data['course_type'] ,
				'citizen_id' => $data['citizen_id'] ,
				'comment' => $data['comment'] ,
				'isBacklist' => $data['isBacklist'] ,
				'blackList_comment' => $data['blackList_comment'] ,
			  	'update_by' => $this->session->userdata('username')
            );
             if($data['birthday'] != null)
            	$this->db->set('birthday', "str_to_date('".$data['birthday']."', '%d/%m/%Y')", FALSE);
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB); 
		}
		
		function updateStudentAddress($data)
		{			
			$dataDB = array(
              	'email' => $data['email'] ,
				'telephone_home' => $data['telephone_home'] ,
				'mobile01' => $data['mobile01'] ,
				'mobile01_network' => $data['mobile01_network'] ,
				'mobile02' => $data['mobile02'] ,
				'mobile02_network' => $data['mobile02_network'] ,
				'add_room_no' => $data['add_room_no'] ,
				'add_home_no' => $data['add_home_no'] ,
				'add_building_name' => $data['add_building_name'] ,
				'add_moo' => $data['add_moo'] ,
				'add_soi' => $data['add_soi'] ,
				'add_road' => $data['add_road'] ,
				'add_district' => $data['add_district'] ,
				'add_area' => $data['add_area'] ,
				'add_province' => $data['add_province'] ,
				'add_post_code' => $data['add_post_code'] ,
				'add_note' => $data['add_note'] ,
				'first_name_writer' => $data['first_name_writer'] ,
				'last_name_writer' => $data['last_name_writer'] ,
				'telephone_writer' => $data['telephone_writer'] ,
				'telephone_network' => $data['telephone_network'] ,
				'update_by' => $this->session->userdata('username')
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB); 
		}
	
		function updateStudentBooking2($data)
		{			
			$dataDB = array(
              	'booking_subject01' => $data['booking_subject01'] ,
				'booking_subject02' => $data['booking_subject02'] ,
				'booking_subject03' => $data['booking_subject03'] ,
				'booking_subject04' => $data['booking_subject04'] ,
				'booking_subject05' => $data['booking_subject05'] ,
				'booking_subject06' => $data['booking_subject06'] ,
				'objective' => $data['objective'] ,
				'study_add_room_no' => $data['study_add_room_no'] ,
				'study_add_home_no' => $data['study_add_home_no'] ,
				'study_add_buliding_name' => $data['study_add_buliding_name'] ,
				'study_add_moo' => $data['study_add_moo'] ,
				'study_add_soi' => $data['study_add_soi'] ,
				'study_add_road' => $data['study_add_road'] ,
				'study_add_distinct' => $data['study_add_distinct'] ,
				'study_add_area' => $data['study_add_area'] ,
				'study_add_province' => $data['study_add_province'] ,
				'study_add_post_code' => $data['study_add_post_code'] ,
				'study_add_note' => $data['study_add_note'] 
            );
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB); 
		}	
	function updateStudentBooking3($data)
		{			
			$dataDB = array(
				'study_time_mon' => $data['study_time_mon'] ,
				'study_time_tue' => $data['study_time_tue'] ,
				'study_time_wed' => $data['study_time_wed'] ,
				'study_time_thr' => $data['study_time_thr'] ,
				'study_time_fir' => $data['study_time_fir'] ,
				'study_time_sat' => $data['study_time_sat'] ,
				'study_time_sun' => $data['study_time_sun'] ,
				'note' => $data['note'] ,
				'known_from' => $data['known_from']
            );
            if($data['study_start_date'] != null)
            $this->db->set('study_start_date', "str_to_date('".$data['study_start_date']."', '%d/%m/%Y')", FALSE);
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB); 
		}
		
	function updateStudentBooking($data)
		{			
			$dataDB = array(
              	'booking_subject01' => $data['booking_subject01'] ,
				'booking_subject02' => $data['booking_subject02'] ,
				'booking_subject03' => $data['booking_subject03'] ,
				'booking_subject04' => $data['booking_subject04'] ,
				'booking_subject05' => $data['booking_subject05'] ,
				'booking_subject06' => $data['booking_subject06'] ,
				'objective' => $data['objective'] ,
				'study_add_room_no' => $data['study_add_room_no'] ,
				'study_add_home_no' => $data['study_add_home_no'] ,
				'study_add_buliding_name' => $data['study_add_buliding_name'] ,
				'study_add_moo' => $data['study_add_moo'] ,
				'study_add_soi' => $data['study_add_soi'] ,
				'study_add_road' => $data['study_add_road'] ,
				'study_add_distinct' => $data['study_add_distinct'] ,
				'study_add_area' => $data['study_add_area'] ,
				'study_add_province' => $data['study_add_province'] ,
				'study_add_post_code' => $data['study_add_post_code'] ,
				'study_add_note' => $data['study_add_note'] ,
				'study_time_mon' => $data['study_time_mon'] ,
				'study_time_tue' => $data['study_time_tue'] ,
				'study_time_wed' => $data['study_time_wed'] ,
				'study_time_thr' => $data['study_time_thr'] ,
				'study_time_fir' => $data['study_time_fir'] ,
				'study_time_sat' => $data['study_time_sat'] ,
				'study_time_sun' => $data['study_time_sun'] ,
				'note' => $data['note'] ,
				'known_from' => $data['known_from'] ,
				'update_by' => $this->session->userdata('username')
            );
            if($data['study_start_date'] != null)
            $this->db->set('study_start_date', "str_to_date('".$data['study_start_date']."', '%d/%m/%Y')", FALSE);
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB); 
		}
		
		function updateStudentImagePath($data)
		{			
			$dataDB = array(
              'student_image' => $data['student_image'] ,
            );
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB); 
		}
		
		function updateStudentAddImagePath($data)
		{			
			$dataDB = array(
              'add_image' => $data['add_image'] ,
            );
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB); 
		}
		
		function updateStudyPath($data) 
		{
			$dataDB = array(
              'study_add_map' => $data['study_add_map'] ,
            );
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB);
		}
		
		function approve($data) 
		{	
			$this->db->set('update_by', "'".$this->session->userdata('username')."'", FALSE);
			$this->db->set('update_date', 'NOW()', FALSE);
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $data);
		}
		
		function getAll()
		{
			$this->db->order_by("first_name", "asc"); 
			$this->db->order_by("last_name", "asc");
			$query = $this->db->get_where('student', array('is_active' => 1));
			return $query->result();
		}
		
		function getAllLimit($limit = NULL, $offset = NULL)
		{
//			$this->output->enable_profiler(TRUE);
//			$this->db->limit($limit, $offset);
//			$this->db->where('school is not null ', NULL, FALSE); 
//			$this->db->where("school != '' ", NULL, FALSE);
//			$this->db->where('first_name is not null ', NULL, FALSE); 
//			$this->db->where("first_name != '' ", NULL, FALSE);
//			$this->db->where('nick_name is not null ', NULL, FALSE); 
//			$this->db->where("nick_name != '' ", NULL, FALSE);
//			$this->db->order_by("first_name", "asc"); 
//			$this->db->order_by("last_name", "asc");
//			$query = $this->db->get_where('student', array('is_active' => 1));
			$sql = "select s.*, o.seq
						from student s, school_order o
						where s.is_active = 1 and s.first_name is not null and s.school is not null and school != '' and nick_name is not null
								and s.school in (select o.school_name from school_order)
						union
							select s.*, '9999' as seq
							from student s 
							where s.is_active = 1 and s.first_name is not null and s.school is not null and school != ''  and nick_name is not null
								and s.school not in (select school_name from school_order)
						order by seq, first_name limit ";
			if($offset != null) {
				$sql .= $offset . ", ";
			}
			$sql .= $limit;
			$this->db->limit($limit, $offset);	
			$query = $this->db->query($sql);		
			return $query->result();
		}
		
		function getAllLimit2($limit = NULL, $offset = NULL)
		{
//			$this->output->enable_profiler(TRUE);
			$this->db->limit($limit, $offset);
			$this->db->where('school is not null ', NULL, FALSE); 
			$this->db->where("school != '' ", NULL, FALSE);
			$this->db->where('first_name is not null ', NULL, FALSE); 
			$this->db->where("first_name != '' ", NULL, FALSE);
			$this->db->where('nick_name is not null ', NULL, FALSE); 
			$this->db->where("nick_name != '' ", NULL, FALSE);
			$this->db->order_by("first_name", "asc"); 
			$this->db->order_by("last_name", "asc");
			$query = $this->db->get_where('student', array('is_active' => 1));
			return $query->result();
		}
		
		function count_all()
	 	{
	 		$this->db->where('school is not null ', NULL, FALSE); 
			$this->db->where("school != '' ", NULL, FALSE);
			$this->db->where('first_name is not null ', NULL, FALSE); 
			$this->db->where("first_name != '' ", NULL, FALSE);
			$this->db->where('nick_name is not null ', NULL, FALSE); 
			$this->db->where("nick_name != '' ", NULL, FALSE);
	 		$query = $this->db->getwhere('student', array('is_active'=>1));
			$count = $query->num_rows(); 
	  		return $count;
	 	}
 
		function getByID($id)
		{
			$query = $this->db->get_where('student', array('student_id' => $id, 'is_active' => 1));
			return $query->row_array();
		}
		
		function getBlackList($key)
		{
			$sql = "SELECT `student_id`,`first_name`,`last_name`,`mobile01`,`create_by`,`student_image` ,`create_date`,`isBacklist`, `blackList_comment` 
					from student 
					where (`isBacklist` = 'Y' and is_active = '1') ";
			if($key['name'] != null)
				$sql .= " and (first_name like '%".$key['name']."%' or last_name like '%".$key['name']."%' )";
			if($key['tel'] != null)
				$sql .= " and (mobile01 like '%".$key['tel']."%' )";	
			if($key['email'] != null)
				$sql .= " and (email like '%".$key['email']."%' )";
			$sql .= " order by first_name, last_name ";	
				
			$query = $this->db->query($sql); 
			return $query->result();
		}
		
		function getByNameLike($key)
		{
			$this->db->like('first_name', $key); 
			$this->db->order_by("first_name", "asc"); 
			$this->db->order_by("last_name", "asc"); 
			$query = $this->db->get_where('student', array('is_active' => 1));
			return $query->result();
		}
		
		function getFromFrontEndByID($id)
		{
			
			$query = $this->db->get_where('student', array('student_id' => $id));
			
			return $query->result();
		}
		
		function getAllFromFrontEnd()
		{
			
			$this->db->order_by("create_date", "asc");
			$this->db->where('status is null ', NULL, FALSE); 
			$query = $this->db->get_where('student', array('is_active' => 1, 'source' => 'C'));
			
			return $query->result();
		}
		
		function delete($newsID)
		{
			$data = array(
               'is_active' => 0,
			   'update_by' => $this->session->userdata('username')   
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('student_id', $newsID);
			$this->db->update('student', $data); 
		}
		
		function deleteComment($newsID, $ment)
		{
			$data = array(
               'is_active' => 0,
				'comment' => $ment,
			   'update_by' => $this->session->userdata('username')   
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('student_id', $newsID);
			$this->db->update('student', $data); 
		}
		
		function tutorlandSearch($criteria)
		{
//			$this->output->enable_profiler(TRUE);
			$sql = 'SELECT * '
		        . ' FROM student stu , student_school stusc '
		        . ' where stu.student_id = stusc.student_id '
		        . " and is_active = '1' " ;
		        
		if($this->session->userdata('profile') != 'Administrator') {
				$sql .= " and stusc.shool_id in ( "
					 ." select eems.school_id "
					 ."	from employee eem, employee_school eems "
					 ."	where eem.employee_id = eems.employee_id "
					 ." and  (eems.student_information = 'Y' or eems.student_contract = 'Y') "
					 ."	and eem.username = '". $this->session->userdata('username') ."' )  ";
	 		} else if($this->session->userdata('profile') == 'Administrator') {
	 			if($criteria['school'] != null) {
	 				$sql .= $criteria['school'];
	 			}
	 			
	 			if($criteria['txtASchool'] != null) {
	 				$sql .= " and stu.school = '" .$criteria['txtASchool'] ."' ";
	 			}
	 			
	 			if($criteria['txtASaveDate'] != null) {
	 				//$this->db->set('birthday', "str_to_date('".$bd."', '%d/%m/%Y')", FALSE);
	 				$sql .= " and ( date(stu.create_date) = str_to_date('".$criteria['txtASaveDate']."', '%d/%m/%Y') or date(stu.update_date) = str_to_date('".$criteria['txtASaveDate']."', '%d/%m/%Y') ) ";
	 			}

	 			if($criteria['txtAMonthBirth'] != null) {
	 				$sql .= " and month(birthday) = " .$criteria['txtAMonthBirth'] ." ";
	 			}
	 			
	 			if($criteria['txtAYearBirth'] != null) {
	 				$sql .= " and year(birthday) = " .($criteria['txtAYearBirth']-543) ." ";
	 			}
	 		}
		        
		        if($criteria['name'] != null)
		        $sql .= " and (stu.first_name like '%" . $criteria['name'] . "%' or stu.last_name like '%" . $criteria['name'] . "%' ) "  ;

		    if($criteria['subject'] != null)
			    $sql .= " and (booking_subject01 = '". $criteria['subject'] ."' or booking_subject02 = '". $criteria['subject'] 
			    	 ."' or booking_subject03 = '". $criteria['subject'] ."' or booking_subject04 = '". $criteria['subject'] 
			    	 ."' or booking_subject05 = '". $criteria['subject'] ."' or booking_subject06 = '". $criteria['subject'] ."') ";
		    if($criteria['class_level'] != null)
		    	$sql .= $criteria['class_level'];
		    if($criteria['location'] != null)
		    	$sql .= " and (study_add_distinct like '%" . $criteria['location'] . "%' or study_add_area like '%" . $criteria['location'] . "%' or study_add_province like '%" . $criteria['location'] . "%' ) "  ;
		    if($criteria['studied_day'] != null) {
		    	if($criteria['studied_day'] == 'จันทร์')
		    		$sql .= " and study_time_mon is not null ";
		    	if($criteria['studied_day'] == 'อังคาร')
		    		$sql .= " and study_time_tue is not null ";
		    	if($criteria['studied_day'] == 'พุธ')
		    		$sql .= " and study_time_wed is not null ";
		    	if($criteria['studied_day'] == 'พฤหัสบดี')
		    		$sql .= " and study_time_thr is not null ";
		    	if($criteria['studied_day'] == 'ศุกร์')
		    		$sql .= " and study_time_fir is not null ";
		    	if($criteria['studied_day'] == 'เสาร์')
		    		$sql .= " and study_time_sat is not null ";
		    	if($criteria['studied_day'] == 'อาทิตย์')
		    		$sql .= " and study_time_sun is not null ";
		    }
		    if($criteria['tel'] != null)
		    	$sql .= " and mobile01 like '%" . $criteria['tel'] . "%' "  ;
		    if($criteria['email'] != null)
		    	$sql .= " and email like '%" . $criteria['email'] . "%' "  ;
		    if($criteria['nick_name'] != null)
		    	$sql .= " and nick_name like '%" . $criteria['nick_name'] . "%' "  ;
		    	 
		    $sql .= ' order by stu.first_name, stu.last_name  ';
				
			$query = $this->db->query($sql); 
			return $query->result();
		}
		
		function getStuForTutor($day){
			$sql = "SELECT stu.*  
					FROM student stu, student_school sc 
					WHERE stu.student_id = sc.student_id 
					and sc.shool_id = 1 
					and stu.is_active = '1' and ( DATEDIFF( NOW( ) , create_date )) <= 90
					and study_add_distinct is not null
					and stu.status = 'approve' ";
			if($day == 'mon')
				$sql .= " and study_time_mon is not null";
			else if($day == 'tue')
				$sql .= " and study_time_tue is not null";
			else if($day == 'wed')
				$sql .= " and study_time_wed is not null";
			else if($day == 'thr')
				$sql .= " and study_time_thr is not null";
			else if($day == 'fri')
				$sql .= " and study_time_fir is not null";
			else if($day == 'sat')
				$sql .= " and study_time_sat is not null";
			else if($day == 'sun')
				$sql .= " and study_time_sun is not null";		
			$sql .=	" order by stu.study_start_date, stu.first_name, stu.last_name limit 500 ";
			$query = $this->db->query($sql); 
			return $query->result();		
		}
		
		function countForCompleteList() {
			$sql = "SELECT stu.*, t.first_name_th, t.tutor_id, t.last_name_th, tm.maching_id, tm.dayy, tm.subject, tm.time, tm.note as match_note
					FROM student stu, student_school sc, tutor t, tutorland_maching tm
					WHERE stu.student_id = sc.student_id
					and stu.student_id = tm.stu_id
					and stu.match_tutor_id = tm.tutor_id
					and stu.match_tutor_id = t.tutor_id
					and stu.maching_status = 'อยู่ระหว่างการเรียนการสอน'
					and sc.shool_id = 1 ";
			$sql .= " and stu.status = 'approve' ";
			$query = $this->db->query($sql);  
			$count = $query->num_rows(); 
	  		return $count;
		}
		
		function selectForCompleteList($limit = NULL, $offset = NULL)
		{
//			$this->output->enable_profiler(TRUE);
			$sql = "SELECT stu.*, t.first_name_th, t.tutor_id, t.last_name_th, tm.maching_id, tm.dayy, tm.subject, tm.time, tm.note as match_note, tm.approve_by
					FROM student stu, student_school sc, tutor t, tutorland_maching tm
					WHERE stu.student_id = sc.student_id
					and stu.student_id = tm.stu_id
					and stu.match_tutor_id = tm.tutor_id
					and stu.match_tutor_id = t.tutor_id and stu.maching_status = 'อยู่ระหว่างการเรียนการสอน' 
					and sc.shool_id = 1 ";
			$sql .= " and stu.status = 'approve' 
					order by tm.maching_date desc, stu.study_start_date, stu.first_name, stu.last_name limit ";
			if($offset != null) {
				$sql .= $offset . ", ";
			}
			$sql .= $limit;
			$query = $this->db->query($sql); 
			return $query->result();
		}
		
		function countFroMatching() {
			$sql = "SELECT stu.*, t.first_name_th, t.tutor_id, t.last_name_th, tm.maching_id, tm.dayy, tm.subject, tm.time, tm.note as match_note
					FROM student stu, student_school sc, tutor t, tutorland_maching tm
					WHERE stu.student_id = sc.student_id 
        and stu.student_id = tm.stu_id 
        and tm.tutor_id  = t.tutor_id
        and sc.shool_id = 1   ";
			$sql .= " AND tm.note IS NULL  ";
			$query = $this->db->query($sql);  
			$count = $query->num_rows(); 
	  		return $count;
		}
		
		function selectForMactchingById($id)
		{//$this->output->enable_profiler(TRUE);
			$sql = "SELECT stu.*, t.first_name_th, t.tutor_id, t.last_name_th, tm.maching_id, tm.dayy, tm.subject, tm.time, tm.note as match_note 
					FROM student stu, student_school sc, tutor t, tutorland_maching tm
					WHERE stu.student_id = sc.student_id 
        and stu.student_id = tm.stu_id 
        and tm.tutor_id  = t.tutor_id
        and sc.shool_id = 1 and tm.maching_id = '". $id. "'  ";
			$sql .= "	order by tm.maching_date, stu.study_start_date, stu.first_name, stu.last_name  ";
			$query = $this->db->query($sql); 
			return $query->result();
		}
		
		function selectForMactching($limit = NULL, $offset = NULL)
		{//$this->output->enable_profiler(TRUE);
			$sql = "SELECT stu.*, t.first_name_th, t.tutor_id, t.last_name_th, tm.maching_id, tm.dayy, tm.subject, tm.time, tm.note as match_note 
					FROM student stu, student_school sc, tutor t, tutorland_maching tm
					WHERE stu.student_id = sc.student_id 
        and stu.student_id = tm.stu_id 
        and tm.tutor_id  = t.tutor_id AND tm.note IS NULL 
        and sc.shool_id = 1   ";
			$sql .= "	order by stu.first_name, stu.last_name limit ";
			if($offset != null) {
				$sql .= $offset . ", ";
			}
			$sql .= $limit;
			$query = $this->db->query($sql); 
			return $query->result();
		}
		
		function selectedStuForTutor($tutorid, $day) 
		{
//			$this->output->enable_profiler(TRUE);
			$sql = "SELECT stu.*, t.first_name_th, t.tutor_id, t.last_name_th, tsd.dayy, tm.maching_id,tsd.t_id, tm.subject as tm_subj
					FROM student stu, student_school sc, tutor t , tutor_student_day tsd, tutorland_maching tm
					WHERE stu.student_id = sc.student_id
					and stu.student_id = tsd.student_id
					and tm.stu_id = sc.student_id 	
					and sc.shool_id = 1 
					and stu.is_active = '1' and tm.status is null ";
			if($tutorid != null) {
				$sql .= " and tsd.tutor_id = " .   $tutorid . " AND t.tutor_id = tsd.tutor_id and tm.tutor_id = t.tutor_id and tm.dayy = tsd.dayy";
			}
			
			$sql .= " and stu.status = 'approve' 
					order by stu.study_start_date, stu.first_name, stu.last_name ";
			$query = $this->db->query($sql); 
			return $query->result();
		}
		
		function cancelBooking($data) 
		{
			$dataDB = array(
              	'maching_status' => $data['maching_status'] ,
			  	'update_by' => $this->session->userdata('username')
            );
            $this->db->set('match_tutor_id', "NULL", FALSE);
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->set('maching_date', 'NOW()', FALSE); 
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB);
			
			$this->db->set('approve_by', "'".$this->session->userdata('username')."'", FALSE);
			$this->db->set('approve_date', 'NOW()', FALSE);
			$this->db->set('status', "'".'reject'."'", FALSE);
			if(isset($data['note'])) {
				$this->db->set('note', "'".$data['note']."'", FALSE);
			}
			$this->db->where('maching_id', $data['maching_id'], FALSE);
			$this->db->update('tutorland_maching');
		}
		
		function removeLog($data){
			// delete log
			$this->db->where('t_id', $data['t_id']);
			$this->db->delete('tutor_student_day');
			
//			$this->db->where('tutor_id', $data['match_tutor_id']);
//			$this->db->where('stu_id', $data['student_id']);
//			$this->db->where('dayy', "'".$data['dayy']."'");
//			$this->db->delete('tutorland_maching');
			
		}
		function saveLog($data) {
			// save log
			$this->db->set('tutor_id', $data['match_tutor_id'], FALSE);
			$this->db->set('student_id', $data['student_id'], FALSE);
			$this->db->set('dayy', "'".$data['dayy']."'", FALSE);
			$this->db->insert('tutor_student_day');
			//$this->db->insert_id();
			
			$this->db->set('tutor_id', $data['match_tutor_id'], FALSE);
			$this->db->set('stu_id', $data['student_id'], FALSE);
			$this->db->set('dayy', "'".$data['dayy']."'", FALSE);
			$this->db->set('subject', "'".$data['subject']."'", FALSE);
			$this->db->set('time', "'".$data['time']."'", FALSE);
			$this->db->set('maching_date', 'NOW()', FALSE);
			$this->db->insert('tutorland_maching');
			return $this->db->insert_id();
		}
		function confirmBooking($data) 
		{
			$dataDB = array(
              	'maching_status' => $data['maching_status'] ,
				'match_tutor_id' => $data['match_tutor_id'] ,
			  	'update_by' => $this->session->userdata('username')
            );
            $this->db->set('maching_date', 'NOW()', FALSE); 
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB);
			
			
		}
		
		function finishBooking($data) 
		{
			$dataDB = array(
              	'maching_status' => $data['maching_status'] ,
			  	'update_by' => $this->session->userdata('username')
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('student_id', $data['student_id']);
			$this->db->update('student', $dataDB);
			
			
			$this->db->set('approve_by', "'".$this->session->userdata('username')."'", FALSE);
			$this->db->set('note', "'".$data['note']."'", FALSE);
			$this->db->set('status', "'".'confirm'."'", FALSE);
			$this->db->set('approve_date', 'NOW()', FALSE);
			$this->db->where('maching_id', $data['t_id'], FALSE);
			$this->db->update('tutorland_maching');
		}
		
	}
?>